﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysShopSettlement
    {
        public int Add(Chain.Model.SysShopSettlement model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysShopSettlement(");
            builder.Append("StartTime,EndTime,RechargeMoney,DrawMoney,PayCard,IsFinish,FinishTime,Remark,OutShopID,UserID,AllExpenseMoney,ProportionMoney,Proportion)");
            builder.Append(" values (");
            builder.Append("@StartTime,@EndTime,@RechargeMoney,@DrawMoney,@PayCard,@IsFinish,@FinishTime,@Remark,@OutShopID,@UserID,@AllExpenseMoney,@ProportionMoney,@Proportion)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@RechargeMoney", SqlDbType.Money, 8), new SqlParameter("@DrawMoney", SqlDbType.Money, 8), new SqlParameter("@PayCard", SqlDbType.Money, 8), new SqlParameter("@IsFinish", SqlDbType.Bit, 1), new SqlParameter("@FinishTime", SqlDbType.DateTime), new SqlParameter("@Remark", SqlDbType.NVarChar, 400), new SqlParameter("@OutShopID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@AllExpenseMoney", SqlDbType.Decimal), new SqlParameter("@ProportionMoney", SqlDbType.Decimal), new SqlParameter("@Proportion", SqlDbType.Decimal) };
            cmdParms[0].Value = model.StartTime;
            cmdParms[1].Value = model.EndTime;
            cmdParms[2].Value = model.RechargeMoney;
            cmdParms[3].Value = model.DrawMoney;
            cmdParms[4].Value = model.PayCard;
            cmdParms[5].Value = model.IsFinish;
            cmdParms[6].Value = model.FinishTime;
            cmdParms[7].Value = model.Remark;
            cmdParms[8].Value = model.OutShopID;
            cmdParms[9].Value = model.UserID;
            cmdParms[10].Value = model.AllExpenseMoney;
            cmdParms[11].Value = model.ProportionMoney;
            cmdParms[12].Value = model.Proportion;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysShopSettlement ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string IDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysShopSettlement ");
            builder.Append(" where ID in (" + IDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysShopSettlement");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,StartTime,EndTime,RechargeMoney,DrawMoney,PayCard,IsFinish,FinishTime,Remark,OutShopID,UserID,AllExpenseMoney,ProportionMoney,Proportion ");
            builder.Append(" FROM SysShopSettlement ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ID,StartTime,EndTime,RechargeMoney,DrawMoney,PayCard,IsFinish,FinishTime,Remark,OutShopID,UserID,AllExpenseMoney,ProportionMoney,Proportion ");
            builder.Append(" FROM SysShopSettlement ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ID desc");
            }
            builder.Append(")AS Row, T.*  from SysShopSettlement T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, string join, out int resCount, params string[] strWhere)
        {
            string tableName = " SysShopSettlement left join SysShop  " + join;
            string[] column = new string[] { "SysShopSettlement.*,SysShop.ShopName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "OutShopID,EndTime", "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.SysShopSettlement GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ID,StartTime,EndTime,RechargeMoney,DrawMoney,PayCard,IsFinish,FinishTime,Remark,OutShopID,UserID,AllExpenseMoney,ProportionMoney,Proportion from SysShopSettlement ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            Chain.Model.SysShopSettlement settlement = new Chain.Model.SysShopSettlement();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ID"] != null) && (set.Tables[0].Rows[0]["ID"].ToString() != ""))
            {
                settlement.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StartTime"] != null) && (set.Tables[0].Rows[0]["StartTime"].ToString() != ""))
            {
                settlement.StartTime = DateTime.Parse(set.Tables[0].Rows[0]["StartTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["EndTime"] != null) && (set.Tables[0].Rows[0]["EndTime"].ToString() != ""))
            {
                settlement.EndTime = DateTime.Parse(set.Tables[0].Rows[0]["EndTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeMoney"] != null) && (set.Tables[0].Rows[0]["RechargeMoney"].ToString() != ""))
            {
                settlement.RechargeMoney = decimal.Parse(set.Tables[0].Rows[0]["RechargeMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawMoney"] != null) && (set.Tables[0].Rows[0]["DrawMoney"].ToString() != ""))
            {
                settlement.DrawMoney = decimal.Parse(set.Tables[0].Rows[0]["DrawMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PayCard"] != null) && (set.Tables[0].Rows[0]["PayCard"].ToString() != ""))
            {
                settlement.PayCard = decimal.Parse(set.Tables[0].Rows[0]["PayCard"].ToString());
            }
            if ((set.Tables[0].Rows[0]["IsFinish"] != null) && (set.Tables[0].Rows[0]["IsFinish"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["IsFinish"].ToString() == "1") || (set.Tables[0].Rows[0]["IsFinish"].ToString().ToLower() == "true"))
                {
                    settlement.IsFinish = true;
                }
                else
                {
                    settlement.IsFinish = false;
                }
            }
            if ((set.Tables[0].Rows[0]["FinishTime"] != null) && (set.Tables[0].Rows[0]["FinishTime"].ToString() != ""))
            {
                settlement.FinishTime = DateTime.Parse(set.Tables[0].Rows[0]["FinishTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Remark"] != null) && (set.Tables[0].Rows[0]["Remark"].ToString() != ""))
            {
                settlement.Remark = set.Tables[0].Rows[0]["Remark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OutShopID"] != null) && (set.Tables[0].Rows[0]["OutShopID"].ToString() != ""))
            {
                settlement.OutShopID = int.Parse(set.Tables[0].Rows[0]["OutShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserID"] != null) && (set.Tables[0].Rows[0]["UserID"].ToString() != ""))
            {
                settlement.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllExpenseMoney"] != null) && (set.Tables[0].Rows[0]["AllExpenseMoney"].ToString() != ""))
            {
                settlement.AllExpenseMoney = Convert.ToDecimal(set.Tables[0].Rows[0]["AllExpenseMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProportionMoney"] != null) && (set.Tables[0].Rows[0]["ProportionMoney"].ToString() != ""))
            {
                settlement.ProportionMoney = Convert.ToDecimal(set.Tables[0].Rows[0]["ProportionMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Proportion"] != null) && (set.Tables[0].Rows[0]["Proportion"].ToString() != ""))
            {
                settlement.Proportion = Convert.ToDecimal(set.Tables[0].Rows[0]["Proportion"].ToString());
            }
            return settlement;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysShopSettlement ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysShopSettlement model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysShopSettlement set ");
            builder.Append("StartTime=@StartTime,");
            builder.Append("EndTime=@EndTime,");
            builder.Append("RechargeMoney=@RechargeMoney,");
            builder.Append("DrawMoney=@DrawMoney,");
            builder.Append("PayCard=@PayCard,");
            builder.Append("IsFinish=@IsFinish,");
            builder.Append("FinishTime=@FinishTime,");
            builder.Append("Remark=@Remark,");
            builder.Append("OutShopID=@OutShopID,");
            builder.Append("UserID=@UserID,");
            builder.Append("AllExpenseMoney=@AllExpenseMoney,");
            builder.Append("ProportionMoney=@ProportionMoney,");
            builder.Append("Proportion=@Proportion");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@RechargeMoney", SqlDbType.Money, 8), new SqlParameter("@DrawMoney", SqlDbType.Money, 8), new SqlParameter("@PayCard", SqlDbType.Money, 8), new SqlParameter("@IsFinish", SqlDbType.Bit, 1), new SqlParameter("@FinishTime", SqlDbType.DateTime), new SqlParameter("@Remark", SqlDbType.NVarChar, 400), new SqlParameter("@OutShopID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@AllExpenseMoney", SqlDbType.Decimal), new SqlParameter("@ProportionMoney", SqlDbType.Decimal), new SqlParameter("@Proportion", SqlDbType.Decimal) };
            cmdParms[0].Value = model.StartTime;
            cmdParms[1].Value = model.EndTime;
            cmdParms[2].Value = model.RechargeMoney;
            cmdParms[3].Value = model.DrawMoney;
            cmdParms[4].Value = model.PayCard;
            cmdParms[5].Value = model.IsFinish;
            cmdParms[6].Value = model.FinishTime;
            cmdParms[7].Value = model.Remark;
            cmdParms[8].Value = model.OutShopID;
            cmdParms[9].Value = model.UserID;
            cmdParms[10].Value = model.ID;
            cmdParms[11].Value = model.AllExpenseMoney;
            cmdParms[12].Value = model.ProportionMoney;
            cmdParms[13].Value = model.Proportion;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public void UpDateSettlement()
        {
            SqlParameter[] parameters = new SqlParameter[0];
            DbHelperSQL.RunProcedure("CP_ShopSettlement", parameters);
        }
    }
}

